package com.cy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.common.exception.ServiceException;
import com.cy.common.vo.BrandList;
import com.cy.common.vo.CategoryList;
import com.cy.common.vo.GoodsList;
import com.cy.common.vo.JsonResult;
import com.cy.common.vo.PageObject;
import com.cy.dao.SysGoodsInfoDao;
import com.cy.dao.SysGoodsListDao;
import com.cy.entity.SysGoodsInfo;
import com.cy.service.SysGoodsListService;

/**  
 * @Title:  SysGoodsListServiceImpl.java   
 * @Package com.cy.service.impl   
 * @Description:    描述   
 * @author: ZHANGSIMIN     
 * @date:   2019年10月20日 下午4:24:54   
 * @version V1.0 
 */
@Service
public class SysGoodsListServiceImpl implements SysGoodsListService{

	@Autowired
	private SysGoodsListDao sysGoodsListDao;
	
	@Override
	public PageObject<GoodsList> getGoodsListByType(String type, Integer id,Integer page,String s) {
		if(type == null || id == null || id == 0) {
			throw new ServiceException("列表分类未选择");
		}
		int typeId = 0,rowCount = 0;
		List<SysGoodsInfo> list;
		int pageSize = 12;
		int startIndex=(page-1)*pageSize;
		if(type.equals("brand")) {
			typeId = 1;
			list = sysGoodsListDao.getGoodsInfoByType(typeId,startIndex,pageSize,id);
			rowCount = sysGoodsListDao.getGoodsCategoryListRowCount(typeId,id);
		}else if(type.equals("category")) {
			typeId = 2;
			Integer[] ids = sysGoodsListDao.getGoodsCategoryList(id);
			list = sysGoodsListDao.getGoodsInfoByType(typeId,startIndex,pageSize,ids);
			rowCount = sysGoodsListDao.getGoodsCategoryListRowCount(typeId,ids);
		}else if(type.equals("search")){
			list = sysGoodsListDao.Search(s,startIndex,pageSize);
		}else {
			throw new ServiceException("列表参数错误");
		}
		List<GoodsList> goodsList = new ArrayList<>();
		for(SysGoodsInfo info : list) {
			GoodsList item = new GoodsList();
			int itemid = info.getId();
			double basePrice = sysGoodsListDao.getGoodsBasePrice(itemid);
			String imgUrl = sysGoodsListDao.getGoodsListImg(itemid);
			item.setGoodsId(itemid);
			item.setGoodsName(info.getGoods_name());
			item.setBasePrice(basePrice);
			item.setImgUrl(imgUrl);
			goodsList.add(item);
		}
		PageObject<GoodsList> pageObject = new PageObject<>();
		pageObject.setRecords(goodsList);
		pageObject.setPageSize(pageSize);
		pageObject.setRowCount(rowCount);
		pageObject.setPageCurrent(page);
		pageObject.setPageCount((rowCount-1)/pageSize+1);
		return pageObject;
	}

	@Override
	public List<BrandList> getBrandList(String type, Integer id) {
		if(type == null || id == null || id == 0) {
			throw new ServiceException("服务器炸了");
		}
		List<BrandList> list;
		if(type.equals("brand")) {
			list = sysGoodsListDao.getBrandListByBrandId(id);
		}else if(type.equals("category")) {
			list = sysGoodsListDao.getBrandListByCateId(id);
		}else {
			throw new ServiceException("参数错误");
		}
		return list;
	}

	@Override
	public List<CategoryList> getCateList(Integer id) {
		int cateid = sysGoodsListDao.getCateId(id);
		Integer[] ids = sysGoodsListDao.getGoodsCategoryList(cateid);
		List<CategoryList> list = sysGoodsListDao.getCateListByCateId(ids);
		return list;
	}

	

	

	

}
